package com.intelligent.eayon.coder;

import com.intelligent.eayon.message.MessageBody;
import com.intelligent.eayon.message.MessageHead;
import com.intelligent.eayon.message.TransMessage;
import com.intelligent.eayon.utils.ByteUtils;
import com.intelligent.eayon.utils.MessageUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class TransMsgDecoder extends ByteToMessageDecoder {

    //private static Logger logger = LoggerFactory.getLogger(TransMsgDecoder.class);

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        try {
            //
            if (in.readableBytes() < 8) {
                return;
            }
            if (in.getByte(8) != 0) {
                if (in.readableBytes() < 74) {
                    return;
                }
                if (in.readableBytes() < in.getInt(69) + 74) {
                    return;
                }
            } else {
                if (in.readableBytes() < 70) {
                    return;
                }
                if (in.readableBytes() < in.getInt(65) + 70) {
                    return;
                }
            }

            boolean resultHead = false;
            boolean resultBody = false;

            if (in.getByte(8) != 0) {
                byte[] head = new byte[74];
                in.getBytes(0, head);
                resultHead = MessageUtils.checksum(head);

                int msgLength = in.getInt(69);

                byte[] body = new byte[msgLength];
                in.getBytes(74, body);
                resultBody = MessageUtils.checksum(body);

            } else {
                byte[] head = new byte[70];
                in.getBytes(0, head);
                resultHead = MessageUtils.checksum(head);

                int msgLength = in.getInt(65);

                byte[] body = new byte[msgLength];
                in.getBytes(70, body);
                resultBody = MessageUtils.checksum(body);
            }

            //check sum
            if (!resultHead || !resultBody) {
//                throw new ApplicationException(ExceptionMessages.ERR_18002);
            }

            TransMessage message = new TransMessage();
            MessageHead head = new MessageHead();
            MessageBody body = new MessageBody();

            head.setVern(in.readBytes(4).array());
            head.setSeqn(in.readBytes(4).array());

            byte ctnf = in.getByte(8);
            head.setCtnf(in.readByte());
            if (ctnf != 0) {
                head.setPakn(in.readBytes(4).array());
            }
            head.setTimp(in.readBytes(16).array());
            head.setMsid(in.readBytes(32).array());
            head.setErrc(in.readBytes(6).array());
            head.setMspr(in.readBytes(2).array());
            head.setMslg(in.readBytes(4).array());
            head.setHcs(in.readByte());

            message.setHead(head);

            int length = ByteUtils.bytes2int(head.getMslg());

            body.setData(in.readBytes(length - 1).array());
            body.setDcs(in.readByte());

            message.setBody(body);
            out.add(message);
//        } catch (ApplicationException e) {
////            throw new ApplicationException(e.getCode());
        } catch (Exception e) {
//            throw new ApplicationException(ExceptionMessages.ERR_18002);
        }

    }
}
